// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui;

import org.dengues.commons.IDenguesCoreContants;
import org.dengues.core.DenguesCorePlugin;
import org.dengues.core.IDesignerUIService;
import org.dengues.core.prefs.IDenguesPrefsConstant;
import org.dengues.core.process.ICompProcess;
import org.dengues.designer.ui.actions.DebugETLProcessAction;
import org.dengues.designer.ui.actions.LaunchEditorAction;
import org.dengues.designer.ui.actions.StopEditorAction;
import org.dengues.designer.ui.editors.DatabaseMultiEditor;
import org.dengues.designer.ui.editors.ProcessMultiEditor;
import org.dengues.designer.ui.process.editors.GEFComponentsEditor;
import org.dengues.designer.ui.process.editors.ProcessEditorInput;
import org.dengues.designer.ui.process.models.CompProcess;
import org.dengues.designer.ui.properties.ComponentPropertyView;
import org.dengues.designer.ui.properties.DenguesEditorPropsLabelProvider;
import org.dengues.model.warehouse.ProcessType;
import org.dengues.ui.DenguesUiPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;

public class DesignerUIService implements IDesignerUIService {

    private LaunchEditorAction runDataBaseAction = new LaunchEditorAction();

    private DebugETLProcessAction debugAction = new DebugETLProcessAction();

    private StopEditorAction stopEditorAction = new StopEditorAction(runDataBaseAction);

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IDesignerPluginService#getEditorsNodeLabelProvider()
     */
    public LabelProvider getEditorsNodeLabelProvider() {
        return new DenguesEditorPropsLabelProvider();

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IDesignerUIService#getLaunchEditorAction()
     */
    public Action getLaunchEditorAction() {
        return runDataBaseAction;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IDesignerUIService#getStopEditorAction()
     */
    public Action getStopEditorAction() {
        return stopEditorAction;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IDesignerUIService#setRunning(boolean)
     */
    public void setRunning(boolean running) {
        stopEditorAction.setEnabled(running);
        runDataBaseAction.setEnabled(running);
        debugAction.setEnabled(running);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IDesignerUIService#addPartListener()
     */
    public void addPartListener() {
        final IWorkbenchWindow activeWorkbenchWindow = DesignerUIPlugin.getDefault().getActiveWorkbenchWindow();
        if (activeWorkbenchWindow != null) {
            activeWorkbenchWindow.getPartService().addPartListener(new IPartListener() {

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
                 */
                public void partActivated(IWorkbenchPart part) {
                    boolean run = setTitle(activeWorkbenchWindow, part);
                    if (!run) {
                        IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
                        if (activePage != null) {
                            IEditorPart activeEditor = activePage.getActiveEditor();
                            run = setTitle(activeWorkbenchWindow, activeEditor);
                        }
                    }
                    setRunning(run);
                }

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
                 */
                public void partBroughtToTop(IWorkbenchPart part) {
                    if (part instanceof ProcessMultiEditor) {
                        ProcessMultiEditor editor = (ProcessMultiEditor) part;
                        activeWorkbenchWindow.getShell().setText(editor.getGefCompEditor().getProcessTitle());
                    } else if (part instanceof DatabaseMultiEditor) {
                        DatabaseMultiEditor editor = (DatabaseMultiEditor) part;
                        activeWorkbenchWindow.getShell().setText(editor.getGefDBEditor().getDatabaseTitle());
                    }
                }

                public void partClosed(IWorkbenchPart part) {
                    boolean openDenguesEditor = DesignerUIPlugin.getDefault().isOpenDenguesEditor();
                    if (!openDenguesEditor) {
                        activeWorkbenchWindow.getShell().setText(DenguesCorePlugin.getDefault().getDenguesTitle("")); //$NON-NLS-1$
                    }
                    setRunning(openDenguesEditor);
                    if (activeWorkbenchWindow.getActivePage() != null) {
                        IViewPart findView = activeWorkbenchWindow.getActivePage().findView(
                                IDenguesCoreContants.VIEW_COMPONENT_PROPERTY_ID);
                        if (findView != null) {
                            if (part instanceof ProcessMultiEditor || part instanceof DatabaseMultiEditor) {
                                ((ComponentPropertyView) findView).clearAll();
                            }
                        }
                    }
                }

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
                 */
                public void partDeactivated(IWorkbenchPart part) {

                }

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
                 */
                public void partOpened(IWorkbenchPart part) {
                }

            });
            activeWorkbenchWindow.addPerspectiveListener(new IPerspectiveListener() {

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPerspectiveListener#perspectiveActivated(org.eclipse.ui.IWorkbenchPage,
                 * org.eclipse.ui.IPerspectiveDescriptor)
                 */
                public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
                    activeWorkbenchWindow.getShell().setText(DenguesCorePlugin.getDefault().getDenguesTitle("")); //$NON-NLS-1$
                }

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPerspectiveListener#perspectiveChanged(org.eclipse.ui.IWorkbenchPage,
                 * org.eclipse.ui.IPerspectiveDescriptor, java.lang.String)
                 */
                public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String changeId) {

                }
            });
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IDesignerUIService#getCompProcessFromType(org.dengues.model.warehouse.ProcessType)
     */
    public ICompProcess getCompProcessFromType(ProcessType type, IPath path) {
        ProcessEditorInput input = new ProcessEditorInput(type, path);
        return input.loadProcess();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IDesignerUIService#addChangeColorPropertyListener()
     */
    public void addChangeColorPropertyListener() {
        DenguesUiPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
             */
            public void propertyChange(PropertyChangeEvent event) {
                if (IDenguesPrefsConstant.DESIGNER_COLOR_BLOCK.equals(event.getProperty())) {
                    final IWorkbenchWindow activeWorkbenchWindow = DesignerUIPlugin.getDefault().getActiveWorkbenchWindow();
                    if (activeWorkbenchWindow != null) {
                        IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
                        if (activePage != null) {
                            IEditorReference[] editorReferences = activePage.getEditorReferences();
                            for (IEditorReference er : editorReferences) {
                                IEditorPart editor = er.getEditor(false);
                                if (editor instanceof ProcessMultiEditor) {
                                    ProcessMultiEditor multiEditor = (ProcessMultiEditor) editor;
                                    GEFComponentsEditor designerEditor = (GEFComponentsEditor) multiEditor.getDesignerEditor();
                                    CompProcess process = designerEditor.getProcess();
                                    process.changeBlockNodeColor((RGB) event.getNewValue());
                                }
                            }
                        }
                    }
                }
            }
        });
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setTitle".
     * 
     * @param activeWorkbenchWindow
     * @param activeEditor
     * @return
     */
    private boolean setTitle(final IWorkbenchWindow activeWorkbenchWindow, IWorkbenchPart activeEditor) {
        boolean run = false;
        if (activeEditor instanceof ProcessMultiEditor) {
            ProcessMultiEditor editor = (ProcessMultiEditor) activeEditor;
            activeWorkbenchWindow.getShell().setText(editor.getGefCompEditor().getProcessTitle());
            run = true;
        } else if (activeEditor instanceof DatabaseMultiEditor) {
            DatabaseMultiEditor editor = (DatabaseMultiEditor) activeEditor;
            activeWorkbenchWindow.getShell().setText(editor.getGefDBEditor().getDatabaseTitle());
            run = true;
        }
        return run;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IDesignerUIService#getDeBugEditorAction()
     */
    public Action getDebugEditorAction() {
        return debugAction;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IDesignerUIService#getCurrentPort()
     */
    public int getCurrentPort() {
        return LaunchEditorAction.getCurrentPort();
    }
}
